// packageI/newMedia/components/comment/comment.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    advertisingt_id:{
      value:10,
      type:Number
    },
    commentsShow:{
      value:false,
      type:Boolean
    },
    autoRequest:{
      value:false,
      type:Boolean
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

    //	getCommentFlag:true,
    mainCommentShow: false,
    maincomment: "",

    comment_total: 0,
    comment_page: 1,
    comment_loading: false,
    comment_total_count: 0,
    StickCommentData: [],

    replyName: "",
    replyContent: "",
    host_comment_id: "",
    second_comment_id: "",

    is_own: false,
    commentManagement: false,
    replyshow: false,
    imgPath: app.globalData.imgPath,
  },

  ready(){
    if(this.data.autoRequest){
      this.getStickComment();
    }
   
  },

  /**
   * 组件的方法列表
   */
  methods: {
    subMainCommentBtn() {
      this.setData({
        maincomment: "",
        mainCommentShow: !this.data.mainCommentShow,
      });
    },
    openComments() {
      this.setData({
        commentsShow: !this.data.commentsShow,
      });
      this.triggerEvent("closeComment");
    },
    subMainBtn() {
      if (!this.data.maincomment) {
        return app.tips("请输入你的精彩评论");
      }
      this.setData({
        mainCommentShow: false,
      });
      this.getReplyComments("main");
    },

    getReplyComments(getType) {
      let dataJson = {};
      let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.comment.comment");
      dataJson.advertisingt_id = this.data.advertisingt_id;
      if (getType == "main") {
        dataJson.comment = this.data.maincomment;
      } else {
        dataJson.comment = this.data.replyContent;
      }
      if (getType == "reply") {
        if (this.data.host_comment_id) {
          dataJson.parent_id = this.data.host_comment_id;
        }
        if (this.data.second_comment_id) {
          dataJson.subordinate_id = this.data.second_comment_id;
        }
      }
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: dataJson,
        success: (resdata) => {
          var res = resdata.data;
          app.tips(res.msg);
          if (res.result == 1) {
            this.getStickComment();
            this.triggerEvent('commentSuccess',{type:'add'});
            if(res.data.is_reward && res.data.is_reward==1){
              this.triggerEvent("reward",res.data);
            }
          } 
        },
        fail: function (res) {},
      });
    },

    getStickComment() {
      this.setData({
        comment_page: 1,
        comment_total: 0,
      });
      let dataJson = {
        advertisingt_id: this.data.advertisingt_id,
      };
      let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.comment.get-comment-list");
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: dataJson,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              comment_total: res.data.last_page,
              StickCommentData: res.data.data,
              comment_total_count: res.data.total,
            });
          } else {
            app.tips(res.msg);
          }
        },
        fail: function (res) {},
      });
    },

    replyCommentsBtn(e) {
      let host_comment_id = e.currentTarget.dataset.hostcommentid;
      let second_comment_id = e.currentTarget.dataset.secondcommentid;
      let uname = e.currentTarget.dataset.uname;
      let content = e.currentTarget.dataset.content;
      let is_own = e.currentTarget.dataset.isown;
      this.setData({
        host_comment_id: host_comment_id,
        second_comment_id: second_comment_id,
        replyContent: "",
        replyName: uname,
        replyshowcontent: content,
      });
      if (is_own == 1) {
        this.setData({
          is_own: true,
        });
      } else {
        this.setData({
          is_own: false,
        });
      }
      this.setData({
        commentManagement: true,
      });
    },

    replyBtn() {
      this.setData({
        replyshow: true,
        commentManagement: false,
      });
    },

    replySubmitBtn() {
      if (!this.data.replyContent) {
        return app.tips("请输入回复内容");
      }
      this.setData({
        replyshow: false,
      });
      this.getReplyComments("reply");
    },

    replyshowClose() {
      this.setData({
        replyshow: false,
      });
    },

    commentManagementClose() {
      this.setData({
        commentManagement: false,
      });
    },

    
    delBtn() {
      if (this.data.host_comment_id && this.data.second_comment_id) {
        this.setData({
          commend_id: this.data.second_comment_id,
        });
      } else {
        this.setData({
          commend_id: this.data.host_comment_id,
        });
      }
      this.setData({
        commentManagement: false,
      });
      this.delReply();
    },
    delReply() {
      let dataJson = {};
      let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.comment.delete-comment");
      dataJson.comment_id = this.data.commend_id;
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: dataJson,
        success: (resdata) => {
          var res = resdata.data;
          app.tips(res.msg);
          if (res.result == 1) {
            this.triggerEvent('commentSuccess',{type:'reduce'});
            this.getStickComment(); 
          }
        },
        fail: function (res) {},
      });
    },



    praiseBtn(e) {
      let host_comment_id = e.currentTarget.dataset.hostcommentid;
      let second_comment_id = e.currentTarget.dataset.secondcommentid;
      let index = e.currentTarget.dataset.index;
      let chilindex = e.currentTarget.dataset.chilindex;
      let is_praise = e.currentTarget.dataset.ispraise;
      if (is_praise == 1) {
        return app.tips("你已赞过该贴");
      }
      this.getPraiseData(host_comment_id, second_comment_id, index, chilindex);
    },
    getPraiseData(host_comment_id, second_comment_id, index, chilindex) {
      let dataJson = {};
      let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.comment.comment-like");

      dataJson.advertisingt_id = this.data.advertisingt_id;

      if (host_comment_id && second_comment_id) {
        dataJson.comment_id = second_comment_id;
      } else {
        dataJson.comment_id = host_comment_id;
      }
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: dataJson,
        success: (resdata) => {
          var res = resdata.data;
          app.tips(res.msg);
          if (res.result != 1) return;

          if (host_comment_id && second_comment_id) {
            this.setData({
              ["StickCommentData.[" + index + "].has_many_comment[" + chilindex + "].is_like"]: 1,
              ["StickCommentData.[" + index + "].has_many_comment[" + chilindex + "].like_count"]: this.data.StickCommentData[index].has_many_comment[chilindex].like_count ?
                this.data.StickCommentData[index].has_many_comment[chilindex].like_count + 1 :
                1,
            });
          } else {
            this.setData({
              ["StickCommentData[" + index + "].is_like"]: 1,
              ["StickCommentData[" + index + "].like_count"]: this.data.StickCommentData[index].like_count ? this.data.StickCommentData[index].like_count + 1 : 1,
            });
          }
        },
        fail: function (res) {},
      });
    },
  


    getMoreStickComment() {
      let json = {
        advertisingt_id: this.data.advertisingt_id,
      };
      if (this.data.comment_loading) return;
      this.setData({
        comment_loading: true,
      });

      let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.comment.get-comment-list");
      if (this.data.comment_page >= this.data.comment_total) {
        this.setData({
          loading: true,
        });
        return;
      } else {
        this.setData({
          comment_page: this.data.comment_page + 1,
        });
        json.page = this.data.comment_page;
        app._postNetWork({
          url: urlStr,
          showToastIn: false,
          data: json,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              var myData = res.data.data;
              this.setData({
                StickCommentData: this.data.StickCommentData.concat(myData),
                comment_loading: false,
              });
            } else {
              this.setData({
                comment_page: this.data.comment_page - 1,
                comment_loading: false,
              });
            }
          },
          fail: function (res) {},
        });
      }
    },





  }
});
